
var exchange_item = 1.0;

$(function() {
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "date1",
	    name: "开票日期",
	    field: "date1",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Date,
	    validator: requiredFieldValidator
	}, {
	    id: "mold",
	    name: "产品",
	    field: "mold_x",
	    width: 150,
	    cssClass: "cell-title",
	    editor: MoldCellEditor
	}, {
	    id: "name",
	    name: "事由",
	    field: "name",
	    width: 120,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "total",
	    name: "应收",
	    field: "total",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "content",
	    name: "备注",
	    field: "content",
	    width: 120,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "status_t",
	    name: "状态",
	    field: "status_t",
	    width: 80,
	    unselectable: true,
	    cssClass: "cell-title"
	}, {
	    id: "ok",
	    name: "",
	    field: "ok",
	    width: 40,
	    cssClass: "cell-effort-driven",	    
	    cannotTriggerInsert: true,
	    formatter: Slick.Formatters.YesNo,
	    editor: Slick.Editors.Checkbox
	}];
	
	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};
		
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
        var item = {total:"0.00", ok: true};
        
        item.date1 = current_date();
        
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
        total();
    });
    
	grid.onCellChange.subscribe(function(e, args) {
		
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
    	
    	item.rmb = fxmul(item.amount, item.rate);
    	
    	grid.updateRow(currentRow);
    	grid.updateRowCount();
    	grid.render();
    	
    	total();
    });
});
